package com.zl.rbac.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zl.rbac.VO.ResultCode;
import com.zl.rbac.VO.ResultVO;
import com.zl.rbac.entity.Permission;
import com.zl.rbac.entity.Role;
import com.zl.rbac.entity.User;
import com.zl.rbac.exception.RbacException;
import com.zl.rbac.service.IRoleService;
import com.zl.rbac.service.IUserService;
import com.zl.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户控制器
 * @ClassName: UserController
 * @author: suke
 * @since: 2021/1/13 9:14
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService  roleService;

    @GetMapping("list")
    @RequiredPermission("查询用户列表")
    public String  list(@RequestParam(defaultValue = "1") Integer pageIndex, Model model) throws Exception {
       Page<User> page =  userService.queryUserAndRolesByPage(pageIndex,2,null);
        model.addAttribute("page", page);
        return "user/list";
    }

    /**
     *
     * @Title:进入到用户添加页面
     * @param
     * @return
     * @author: suke
     * @since: 2021/1/13 13:42
     */
    @GetMapping("save")
    @RequiredPermission("进入用户添加页面")
    public String  toSavePage(Model model)  throws  Exception{
        //查询所有角色, 把权限保存request域
        List<Role> roleList =  roleService.queryAll();
        model.addAttribute("unSelectedRoles",roleList);
        return "user/edit";
    }


    /**
     *
     * @Title: 处理添加与修改的方法
     * @param user  角色对象
     * @param roleIds  选择的一组权限id
     * @return java.lang.String
     * @author: suke
     * @since: 2021/1/12 10:09
     */
    @PostMapping("saveOrUpdate")
    @RequiredPermission("用户添加与修改")
    public String  saveOrUpdate(User user, Integer[] roleIds) throws Exception{
        //区分是添加还是修改,  判断role的id是否有值,  有: 修改   没有: 添加
        if(user.getId() == null || user.getId() ==  0){  // 添加
            userService.save(user,roleIds);
        }else{ //修改
            userService.update(user,roleIds);
        }
        return  "redirect:/user/list";  //重定向到    public String  list 处理方法
    }


    @GetMapping("update")
    @RequiredPermission("进入用户修改页面")
    public String  toPpdatePage(Integer userid,Model model)  throws  Exception{

        //查询修改的用户的信息以及该用户角色信息
        User user = userService.queryUserAndRolesById(userid);
        model.addAttribute("user",user);

        //角色拥有的权限
        List<Role> selectedRoles =user.getRoleList();

        //查询未选权限, 1) 编写sql查询  SELECT *  FROM tb_permission  WHERE id  NOT IN()
        //  2) 代码逻辑判断
        List<Role> roleList =  roleService.queryAll();

        if(selectedRoles.size() == 0){
            model.addAttribute("unSelectedRoles",roleList);
        }else{
            //创建未选的权限集合
            List<Role> unSelectedRoles = new ArrayList<>();
            for(Role role: roleList){
                if(!selectedRoles.contains(role)){
                    unSelectedRoles.add(role);
                }
            }
            model.addAttribute("unSelectedRoles",unSelectedRoles);
        }
        //查询角色已选权限, 已知条件:  角色id
        return "user/edit";
    }


    @RequestMapping("delete")
    @RequiredPermission("删除用户")
    public String delete(Integer userid) throws Exception{
        userService.delete(userid);
        return  "redirect:/user/list";
    }






}
